/**
 * 支付订单实体类 - 支付业务的核心实体，记录支付订单的完整生命周期
 *
 * 开发人员：陈宏伟
 * 开发时间：2025-10-25 至 2025-10-30
 * 模块职责：定义支付订单的业务属性和状态管理，作为支付系统的数据核心
 *
 * 技术要点：
 * - 多租户关联设计，与企业、用户、产品实体关联
 * - 支付状态枚举管理，支持多种支付状态流转
 * - 金额精度控制，使用BigDecimal确保财务准确性
 * - 审计字段自动维护，记录支付时间和关闭时间
 * - 唯一约束确保订单号不重复
 *
 * 开发故事：负责支付订单实体的数据模型设计，与支付服务层深度配合，实现支付订单的完整生命周期管理。
 */
package com.pbl.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.pbl.common.enums.PaymentStatus;
import lombok.*;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "payment_orders")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class PaymentOrder {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "enterprise_id", nullable = false)
    private Enterprise enterprise;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id")
    private Product product;

    @Column(name = "out_trade_no", nullable = false, length = 64, unique = true)
    private String outTradeNo;

    @Column(name = "trade_no", length = 64, unique = true)
    private String tradeNo;

    @Column(nullable = false, length = 128)
    private String name;

    @Column(name = "amount", nullable = false, precision = 12, scale = 2)
    private BigDecimal amount;

    @Column(name = "pay_type", nullable = false, length = 16)
    private String payType; // wxpay/alipay

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 16)
    private PaymentStatus status;

    @Column(name = "client_ip", length = 45)
    private String clientIp;

    @Column(name = "notify_url", length = 255)
    private String notifyUrl;

    @Column(name = "return_url", length = 255)
    private String returnUrl;

    @Column(name = "pid", length = 64)
    private String pid;

    @Column(name = "cid", length = 32)
    private String cid;

    @Column(name = "param", length = 255)
    private String param;

    @Column(name = "sign_type", length = 16)
    private String signType;

    @Column(name = "paid_at")
    private LocalDateTime paidAt;

    @Column(name = "closed_at")
    private LocalDateTime closedAt;

    @Column(name = "created_at", insertable = false, updatable = false)
    private LocalDateTime createdAt;

    @Column(name = "updated_at", insertable = false, updatable = false)
    private LocalDateTime updatedAt;
}
